package client.control;


import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import shop.local.valueobjects.Warenkorb;

public class InfoPanel extends JPanel {
	
	/**
	 * Create the panel.
	 */
	
	DefaultTableModel model;
	private JTable table;
	private JPanel contentPane;
	
	public InfoPanel() {
		contentPane = new JPanel();
		add(contentPane);
	}
	
	public void showWarenkorb(Warenkorb liste) {
		contentPane.removeAll();
		
		contentPane.setLayout(new MigLayout("", "[]", "[][]"));
		
		JLabel lblIhrWarenkorb = new JLabel("Ihr Warenkorb");
		add(lblIhrWarenkorb, "cell 0 0");
		
		model = new NoEditModel();
		if (table != null) {
			remove(table);
		}
		table = new JTable(model);
		table.setColumnSelectionAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
		table.setRowSorter(sorter);
		
		contentPane.add(table.getTableHeader(), "cell 0 1");
		
		contentPane.add(table, "cell 0 2");

		model.addColumn("Name");
		table.getColumn("Name").setWidth(120);
		model.addColumn("Bestand");
		table.getColumn("Bestand").setWidth(20);
		model.addColumn("Preis");
		table.getColumn("Preis").setWidth(60);
		
		int k = 0;		
		for(int i = 1; i <= liste.size(); i++) {
			Object[] einArtikel = new Object[3];
			
			einArtikel[0] = liste.get(i).getTitel();
			einArtikel[1] = liste.get(i).getBestand();
			einArtikel[2] = liste.get(i).getPreis();
			model.insertRow(k++, einArtikel);			 
		}
		
		contentPane.repaint();
		contentPane.validate();
	}
}
